﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Milan.Diagnostics;
using System.Threading;

namespace Milan.Forms
{
	public partial class UserLogDisplay : UserControl, IDisposable, ILog
	{
		public UserLogDisplay()
		{
			InitializeComponent();
		}
		public new void Dispose()
		{
			log.UnregisterDisplayLogger(this);
		}
		delegate void WriteMessageDelegate(string line, LogLevel logLevel);
		public void WriteLocal(string line, LogLevel logLevel)
		{
			if (logLevel == LogLevel.Error)
				richTextBoxLog.ForeColor = Color.Red;
			else if (logLevel == LogLevel.Warning)
				richTextBoxLog.ForeColor = Color.Yellow;
			else
				richTextBoxLog.ForeColor = Color.White;
			richTextBoxLog.AppendText(line);
		}
		public void Write(string text, LogLevel logLevel)
		{
			Invoke(new WriteMessageDelegate(WriteLocal), text, logLevel);
		}
		public void WriteLine(string line, LogLevel logLevel)
		{
			Invoke(new WriteMessageDelegate(WriteLocal), line + "\n", logLevel);
		}
		protected override void OnCreateControl()
		{
			base.OnCreateControl();
			this.ParentForm.FormClosing += new FormClosingEventHandler(ParentForm_FormClosing);
			log.RegisterDisplayLogger(this);
		}
		void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			log.UnregisterDisplayLogger(this);
		}
	}
}
